package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.CommonUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyQrCodeAddBo;
import com.ruoyi.system.bo.ZsyyQrCodeQueryBo;
import com.ruoyi.system.bo.ZsyyQrCodeEditBo;
import com.ruoyi.system.domain.ZsyyQrCode;
import com.ruoyi.system.mapper.ZsyyQrCodeMapper;
import com.ruoyi.system.vo.ZsyyQrCodeVo;
import com.ruoyi.system.service.IZsyyQrCodeService;
import com.ruoyi.system.mapper.ZsyyQrCodeMapper;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 二维码管理Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyQrCodeServiceImpl extends ServicePlusImpl<ZsyyQrCodeMapper, ZsyyQrCode> implements IZsyyQrCodeService {
    @Autowired
    private ZsyyQrCodeMapper iZsyyQrCodeMapper;
    @Autowired
    private GetCommunityIdImpl getCommunityIdImpl;
	@Autowired
	private CreateQrCodeService createQrCodeService;

    @Override
    public ZsyyQrCodeVo queryById(Integer id){
        return getVoById(id, ZsyyQrCodeVo.class);
    }

    @Override
    public TableDataInfo<ZsyyQrCodeVo> queryPageList(ZsyyQrCodeQueryBo bo) {
    	bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
		Page<ZsyyQrCodeVo> page = iZsyyQrCodeMapper.queryPageList(PageUtils.buildPage(),bo);
		return PageUtils.buildDataInfo(page);
    }

    @Override
    public List<ZsyyQrCodeVo> queryList(ZsyyQrCodeQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyQrCodeVo.class);
    }

    private LambdaQueryWrapper<ZsyyQrCode> buildQueryWrapper(ZsyyQrCodeQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyQrCode> lqw = Wrappers.lambdaQuery();
		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
        lqw.like(StrUtil.isNotBlank(bo.getRemarks()), ZsyyQrCode::getRemarks, bo.getRemarks());
        lqw.eq(!CommonUtil.isEmpty(bo.getCommunityId()), ZsyyQrCode::getCommunityId, bo.getCommunityId());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyQrCodeAddBo bo) {
        ZsyyQrCode add = BeanUtil.toBean(bo, ZsyyQrCode.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyQrCodeEditBo bo) {
        ZsyyQrCode update = BeanUtil.toBean(bo, ZsyyQrCode.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyQrCode entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
